package com.moweilin.folded_binding;

import de.jensd.fx.glyphs.GlyphIcon;
import org.apache.pdfbox.multipdf.LayerUtility;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
import org.apache.pdfbox.util.Matrix;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.File;
import java.io.IOException;
import java.util.List;

import static java.lang.Math.PI;

/**
 * Created by moweilin on 2017/1/11.
 */
public class PDFReader {
    private String file;
    private int pageCount;
    public PDFReader(String file) {
        this.file = file;
        try {
            read(file);
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    public PDFReader() {
    }

    public void read(String file) throws IOException {
        PDDocument pdDocument = PDDocument.load(new File(file));
        int count = pdDocument.getNumberOfPages();
        this.pageCount = count;
//        System.out.println("pdf count: " +count);
//        PDPage pdPage = pdDocument.getPage(1);
//        System.out.println(pdPage.getContents());
//        PDDocumentCatalog pdDocumentCatalog =  pdDocument.getDocumentCatalog();
//        PDPageLabels pdPageLabels = pdDocumentCatalog.getPageLabels();
//        pdPageLabels.getPageIndicesByLabels().forEach((name,key) ->{
//            System.out.println(name + ":::" + key);
//        });

        pdDocument.close();

    }

    public int getPageCount() {
        return pageCount;
    }

   public void copyByIndex(List<Integer> indexList,String saveFilePath) throws IOException {
       PDDocument pdDocument = PDDocument.load(new File(file));
       PDDocument newDocument = new PDDocument();
//       indexList.forEach(index ->{
//           PDPage page = null;
//
//           if(index < pageCount){
//               page =pdDocument.getPage(index);
////               setPageMargin(page,500.0f);
//           }else {
//               page = new PDPage();
//           }
//           newDocument.addPage(page);
//       });

       PDDocument document1 = new PDDocument();
       PDPage page1 = new PDPage();
       document1.addPage(page1);
       PDRectangle rectangle1 = PDRectangle.A5;
       page1.setBleedBox(rectangle1);
       page1.setCropBox(rectangle1);
       page1.setTrimBox(rectangle1);
       page1.setMediaBox(rectangle1);
       PDPageContentStream stream1 = new PDPageContentStream(document1,page1);
       stream1.addRect(0,0,rectangle1.getWidth(),rectangle1.getHeight());
       stream1.setNonStrokingColor(Color.CYAN);
       stream1.fill();
       stream1.close();
       LayerUtility layerUtility = new LayerUtility(newDocument);
       for (int i = 0; i < indexList.size() - 1; i += 2) {

           PDRectangle rectangle = PDRectangle.A4;
           PDPage newPage = new PDPage(rectangle);
           newPage.setTrimBox(rectangle);
           newPage.setCropBox(rectangle);
           newPage.setBleedBox(rectangle);
           newPage.setMediaBox(rectangle);

           newDocument.addPage(newPage);
//           newPage.setRotation(90);
           PDPageContentStream newStream = new PDPageContentStream(newDocument,newPage);

           writeForm(layerUtility,pdDocument,indexList.get(i),newStream,0,rectangle.getHeight(),1.5 * PI);
           writeForm(layerUtility,pdDocument,indexList.get(i + 1),newStream,0,rectangle.getHeight() / 2,1.5 * PI);
           newStream.close();
       }


       newDocument.save(saveFilePath);
       newDocument.close();
       pdDocument.close();

   }
   public void writeForm(LayerUtility layerUtility,PDDocument sourceDocument,int pageIndex,PDPageContentStream newStream,float x,float y,double theta) throws IOException {
       PDFormXObject form =  buildForm(layerUtility,sourceDocument,pageIndex);
       AffineTransform transform = new AffineTransform();
       double ration = PDRectangle.A4.getWidth() / PDRectangle.A4.getHeight();

       transform.translate(x,y);
       transform.rotate(theta);
       transform.scale(ration,ration);

       form.setMatrix(transform);
       newStream.drawForm(form);
   }
   private PDFormXObject buildForm(LayerUtility layerUtility,PDDocument sourceDocument, int index) throws IOException {

       PDPage page = null;
       if(index < sourceDocument.getNumberOfPages()){
           page =sourceDocument.getPage(index);
//               setPageMargin(page,500.0f);
       }else {
           page = new PDPage();
       }

//       PDPageContentStream stream = new PDPageContentStream(sourceDocument,page);
//       stream.addRect(0,0,rectangle.getWidth(),rectangle.getHeight());
//       stream.setNonStrokingColor(color);
//       stream.fill();
//       stream.close();
       return layerUtility.importPageAsForm(sourceDocument,page);
   }
   protected void mergePages(){

   }

   protected void setPageMargin(PDPage page,float margin){
       PDRectangle rectangle = PDRectangle.A4;
       rectangle.setLowerLeftX(margin);
        page.setCropBox(rectangle);
   }
}
